 
import { Matrix3 } from "GameLoop/Execution/FormationManager/types";
import { PositionHelper } from "utils/position/PositionHelper";


export class planningHelper
{
    static fastUpgradePlanMatrix(controllerpos:RoomPosition,block_map:Record<number,boolean>)
    {
        

        const room = Game.rooms[controllerpos.roomName];
        const roomName = controllerpos.roomName;
        const standle:Record<number,number>={};
        const isplaceable=(x:number,y:number,record:Record<number,number>)=>
        {
            for(let i=x;i<x+4;i++)
            {
                for(let j=y;j<y+4;j++)
                {
                    if(room.getTerrain().get(i,j) == TERRAIN_MASK_WALL)
                    {
                        return  false;
                    }
                    if(block_map[i+j*50])
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        const getplacePosition = (controlerpos:RoomPosition)=>
        {
            let offset_a=[-4,-3,-2,-1,0,1];
            let offset_b=[-4,1];
            for(const x of offset_a)
            {
                for(const y of offset_b)
                {
                    if(isplaceable(x+controlerpos.x,y+controlerpos.y,standle))
                    {
                        let position = PositionHelper.New(x+controlerpos.x,y+controlerpos.y,roomName);
                        return position;
                    }
                    if(isplaceable(y+controlerpos.x,x+controlerpos.y,standle))
                    {
                        let  position = PositionHelper.New(y+controlerpos.x,x+controlerpos.y,roomName);
                        return position;
                    }
                }
            }
            return undefined;
        }
        let pos= getplacePosition(controllerpos);

        if(pos)
        {
            Memory.Debug[`${controllerpos.roomName}_ 快速升级放置点`] = `控制器:${JSON.stringify(controllerpos)} -- 放置点 :${JSON.stringify(pos)}`;
            /**
             *      ---------->x
             *      |
             *      |
             *      |
             *    y V
             *
            */
            //
           return this.getfastUpgradePlanMatrix(controllerpos,pos);
        }
        return false;
    }

    /**
     *
     * @param controllerpos 控制器坐标
     * @param placepos 放置点左上角坐标
     * @returns
     */
    public static getfastUpgradePlanMatrix(controllerpos:IFVector2,placepos:IFVector2)
    {
        const getPlaceRotatorMatrix=(placepos:IFVector2,pos:IFVector2)=>
        {
            const offset_y = placepos.y-pos.y;
            const offset_x = placepos.x-pos.x;
            if(offset_x>=0)
            {
                if(offset_y>=0)
                {
                    // 1象限
                    return new Matrix3();
                   
                }
                else
                {
                    // 4象限
                    return new Matrix3().makeRotator(Math.PI/2);
                }
            }
            else
            {
                if(offset_y>=0)
                {
                    // 2象限
                 
                    return new Matrix3().makeRotator(-Math.PI/2);
                }
                else
                {
                     // 3象限
                     return new Matrix3().makeRotator(Math.PI);
                }
            }
             
        }
        const ratator = getPlaceRotatorMatrix(placepos,controllerpos);
        const translate = new Matrix3().makeTranslate({x:placepos.x+1.5,y:placepos.y+1.5})
        const translate2 = new Matrix3().makeTranslate({x:-1.5,y:-1.5});
         translate2.multplay(ratator).multplay(translate);
        return translate2;
    }
}
